<template>
<div class="page-container">
    <!--工具栏-->
    <div class="toolbar" style="padding-left:15px;">
        <el-form :inline="true" :model="filters" :size="size" ref="filters">
            <el-row class="t_r button">
                <el-form-item>
                    <span>商家</span>
                    <el-button v-model="filters.businessName" class="business" @click="handleBusiness">{{filters.businessName}}</el-button>
                </el-form-item>
            </el-row>
            <el-row class="search-input">
                <el-form-item prop="code" label="供应商名称：">
                    <el-input v-model="filters.code" placeholder="供应商名称"></el-input>
                </el-form-item>
                <el-form-item prop="name" label="供应商编码：">
                    <el-input v-model="filters.name" placeholder="供应商编码"></el-input>
                </el-form-item>
                <el-form-item prop="shopid" label="采购方式：">
                    <el-select v-model="filters.status" placeholder="采购方式" style="width:215px">
                        <el-option label="普通采购" value="0"></el-option>
                        <el-option label="集团采购" value="1"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item prop="shopid" label="状态">
                    <el-select v-model="filters.t3" placeholder="状态" style="width:190px">
                        <el-option label="启用" value="0"></el-option>
                        <el-option label="关闭" value="1"></el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="时间安排">

                    <el-date-picker v-model="filters.dateTime" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期">
                    </el-date-picker>
                    <!-- <el-date-picker
              v-model="filters.dateTime"
              type="daterange"
              align="right"
              unlink-panels
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :picker-options="pickerOptions"
              style="width:415px"
            ></el-date-picker> -->
                </el-form-item>

            </el-row>
            <el-row class="search-button">
                <el-form-item>
                    <kt-button icon="fa fa-search" :label="$t('action.search')" type="primary" @click="findPage(null)" />
                </el-form-item>
                <el-form-item>
                    <kt-button icon="fa fa-refresh" :label="$t('action.reset')" type="primary" @click="resetForm('filters')" />
                </el-form-item>
            </el-row>
        </el-form>
    </div>
    <div class="toolbar" style="float:right;padding-top:20px;padding-right:15px;">
        <!--表格显示列界面-->
        <table-column-filter-dialog ref="tableColumnFilterDialog" :columns="columns" @handleFilterColumns="handleFilterColumns"></table-column-filter-dialog>
    </div>
    <!--表格内容栏-->
    <cy-table :height="350" :data="pageResult" :columns="filterColumns" @findPage="findPage" @selectionChange="selectionChange" @handleDelete="handleDelete" @handleEdit="handleEdit" ref="CyTable"></cy-table>
    <!--新增编辑界面-->
    <el-dialog title="换货处理" width="40%" :visible.sync="editDialogVisible" :close-on-click-modal="false">
        <el-form :model="dataForm" label-width="100px" :rules="dataFormRules" ref="dataForm" :size="size">
            <el-row class="searchDialog-input">
                <el-form-item label="id：" prop="id" v-if="false">
                    <el-input v-model="dataForm.id" :disabled="true" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="换货服务编号：" class="styleReplace" prop="serveno">
                    <el-input v-model="dataForm.serveno" auto-complete="off" :disabled="true"></el-input>
                </el-form-item>
                <el-form-item label="状态:" prop="status" class="styleReplace">
                    <el-select v-model="dataForm.status" placeholder="请选择状态" clearable style="width:100%">
                        <el-option v-for="item in statusList" :key="item.id" :label="item.val" :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
            </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer search-button">
            <el-button :size="size" @click.native="editDialogVisible = false">{{$t('action.cancel')}}</el-button>
            <el-button :size="size" type="primary" @click.native="submitForm" :loading="editLoading">{{$t('action.submit')}}</el-button>
        </div>
    </el-dialog>
    <!--选择商家-->
    <el-dialog title="选择商家" width="60%" height="60%" :visible.sync="choiceBusinessVisible" :close-on-click-modal="false">
        <el-form :model="dataForm" label-width="90px" :rules="dataFormRules" ref="dataForm" :size="size" label-position="right" inline>
            <el-form-item label="ID：" prop="id" v-if="false">
                <el-input v-model="dataForm.id" :disabled="true" auto-complete="off" placeholder="请输入"></el-input>
            </el-form-item>
            <el-row class="searchDialog-input">
                <el-form-item label="商家编码：" prop="number">
                    <el-input v-model="dataForm.number" auto-complete="off" placeholder="请输入"></el-input>
                </el-form-item>
                <el-form-item label="商家名称：" prop="mobile">
                    <el-input v-model="dataForm.mobile" auto-complete="off" placeholder="请输入"></el-input>
                </el-form-item>
            </el-row>
            <el-row class="search-button">
                <el-form-item>
                    <kt-button icon="fa fa-search" :label="$t('action.search')" type="primary" @click="findPage(null)" />
                </el-form-item>
                <el-form-item>
                    <kt-button icon="fa fa-refresh" :label="$t('action.reset')" type="primary" @click="resetForm('filters')" />
                </el-form-item>
            </el-row>
        </el-form>
        <!--表格内容栏-->
        <cy-table :data="pageResult" :columns="choiceBusinessFilterColumns" @findPage="choiceBusinessFindPage" @selectionChange="selectionChange" @handleDelete="handleDelete" @rowClick="rowClick" ref="CyTable"></cy-table>
        <div slot="footer" class="dialog-footer" style="text-align:right">
            <el-button :size="size" @click.native="choiceBusinessVisible = false">{{$t('action.cancel')}}</el-button>
        </div>
    </el-dialog>
</div>
</template>

<script>
import CyTable from "@/views/Core/CyTable";
import KtButton from "@/views/Core/KtButton";
import {
    format
} from "@/utils/datetime";
import TableColumnFilterDialog from "@/views/Core/TableColumnFilterDialog";

export default {
    components: {
        CyTable,
        KtButton,
        TableColumnFilterDialog
    },
    data() {
        return {
            readonly: true,
            size: "small",
            filters: {
                businessName: "",
                code: "",
                name: "",
                dateTime: null,
                status: "",
                res: {
                    total: 1,
                    code: "0000",
                    rows: [{
                        t1: "210008",
                        t2: "京东",
                        t3: "第三方电商",
                        t4: "BBC",
                        t5: "线上",
                        t6: "开启",
                        t7: "开启",
                        t8: "开启",
                        t9: "2019-12-24 14:58:59",
                        t10: "开启",
                        t11: "开启"
                    }]
                }
            },
            columns: [{
                    prop: "t1",
                    label: "商家名称",
                    minWidth: 120
                },
                {
                    prop: "t2",
                    label: "供应商编号",
                    minWidth: 120
                },
                {
                    prop: "t3",
                    label: "交易币别",
                    minWidth: 120
                },
                {
                    prop: "t4",
                    label: "供应商名称",
                    minWidth: 120
                },
                {
                    prop: "t5",
                    label: "公司名称",
                    minWidth: 120
                },
                {
                    prop: "t6",
                    label: "采购方式",
                    minWidth: 120
                },
                {
                    prop: "t7",
                    label: "状态",
                    minWidth: 120
                },
                {
                    prop: "t8",
                    label: "主营品类",
                    minWidth: 120
                },
                {
                    prop: "t9",
                    label: "创建时间",
                    minWidth: 180
                },
                {
                    prop: "t10",
                    label: "数据来源",
                    minWidth: 120
                },
                {
                    prop: "t11",
                    label: "备注",
                    minWidth: 120
                }
            ],
            filterColumns: [],
            choiceBusinessFilters: {
                businessName: "",
                code: "",
                name: "",
                status: "",
                res: {
                    total: 1,
                    code: "0000",
                    rows: [{
                            t1: "111",
                            t2: "录屏商家",
                            t3: "333"
                        },
                        {
                            t1: "444",
                            t2: "商家2",
                            t3: "666"
                        }
                    ]
                }
            },

            choiceBusinessColumns: [{
                    prop: "t1",
                    label: "商家编号",
                    minWidth: 120
                },
                {
                    prop: "t2",
                    label: "商家名称",
                    minWidth: 120
                },
                {
                    prop: "t3",
                    label: "商家类型",
                    minWidth: 120
                }
            ],
            choiceBusinessFilterColumns: [],
            pageRequest: {
                pageNum: 1,
                pageSize: 10
            },
            pageResult: {},

            operation: false, // true:新增, false:编辑
            editDialogVisible: false, // 新增编辑界面是否显示
            choiceBusinessVisible: false, // 选择商家
            editLoading: false,
            dataFormRules: {
                replyContent: [{
                    required: true,
                    message: "回复内容",
                    trigger: "blur"
                }]
            },
            // 新增编辑界面数据
            dataForm: {
                serveno: "",
                status: ""
            },
            shopList: [],
            statusList: [],
            checked: []
        };
    },
    watch: {
        choiceBusinessVisible(newVal, OblVal) {
            this.dataForm = {};
        }
    },
    methods: {
        // 获取分页数据
        findPage: function (data) {
            this.$refs.CyTable.findPageStaticState(this.filters);
        },
        choiceBusinessFindPage: function (data) {
            this.$refs.CyTable.findPageStaticState(this.choiceBusinessFilters);
        },
        // 显示新增界面
        handleAdd: function () {
            this.editDialogVisible = true;
            this.operation = true;
            this.resetForm("dataForm");
        },
        //商家-弹框
        handleBusiness: function () {
            this.choiceBusinessVisible = true;
        },
        rowClick: function (t_row) {
            console.log("111------------------------");
            console.log(t_row.row.t2);
            this.choiceBusinessVisible = false;
            this.filters.businessName = t_row.row.t2;
            console.log("222------------------------");
        },
        //选择框
        selectionChange: function (params) {
            console.log("111------------------------");
            console.log(params);
            this.checked = params.selections;
        },
        // 批量删除
        handleDelete: function (data) {
            if (data != null && data.params != null && data.params.length > 0) {
                let ids = data.params.map(item => item.id).toString();

                var params = {};
                params.t = "changeProduct";
                params.ids = ids;
                var this_ = this;
                this.utils.request.deleteUserInfo(params, function (res) {
                    if (res.code == "0000") {
                        this_.$message({
                            message: "操作成功",
                            type: "success"
                        });
                        this_.findPage(null);
                    } else {
                        this_.$message({
                            message: "操作失败, " + res.msg,
                            type: "error"
                        });
                    }
                });
            }
        },
        // 显示换货处理
        handleEdit: function () {
            if (this.checked.length == 1) {
                this.editDialogVisible = true;
                this.dataForm.serveno = this.checked[0].serveno;
                this.dataForm.status = this.checked[0].status;
                this.dataForm.id = this.checked[0].id;
            } else {
                this.$message({
                    message: "退款退货处理必须选中一条数据",
                    type: "error"
                });
            }
        },
        // 编辑
        submitForm: function () {
            this.$refs.dataForm.validate(valid => {
                if (valid) {
                    this.$confirm("确认提交吗？", "提示", {}).then(() => {
                        let params = Object.assign({}, this.dataForm);
                        params.t = "changeProduct";
                        var this_ = this;
                        this.utils.request.editInfo(params, function (res) {
                            if (res.code == "0000") {
                                this_.$message({
                                    message: "操作成功",
                                    type: "success"
                                });

                                this_.$refs["dataForm"].resetFields();

                                this_.editDialogVisible = false;

                                this_.findPage(null);
                            } else {
                                this_.$message({
                                    message: "操作失败, " + res.msg,
                                    type: "error"
                                });
                            }
                        });
                    });
                }
            });
        },

        // 时间格式化
        dateFormat: function (row, column, cellValue, index) {
            return format(row[column.property]);
        },

        resetForm(formName) {
            this.dataForm = {};
            if (this.$refs[formName] != undefined) {
                this.filters.start_time = "";
                this.filters.end_time = "";
            }
        },

        // 处理表格列过滤显示
        displayFilterColumnsDialog: function () {
            this.$refs.tableColumnFilterDialog.setDialogVisible(true);
        },
        // 处理表格列过滤显示
        handleFilterColumns: function (data) {
            this.filterColumns = data.filterColumns;
            this.$refs.tableColumnFilterDialog.setDialogVisible(false);
        },
        // 处理表格列过滤显示
        initColumns: function () {
            let temp = [];

            $.each(this.columns, function (key, val) {
                temp.push(val);
            });
            this.filterColumns = temp;

            let temp2 = [];
            $.each(this.choiceBusinessColumns, function (key, val) {
                temp2.push(val);
            });
            this.choiceBusinessFilterColumns = temp2;

            this.filters.businessName = this.choiceBusinessFilters.res.rows[0].t2;
        },

        //列表下载
        downloadExcel() {
            this.$confirm("确定下载列表文件?", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                })
                .then(() => {
                    let filename = "换货管理下载";
                    this.$refs.CyTable.exportData(
                        this.filters,
                        this.filterColumns,
                        filename
                    );
                })
                .catch(() => {});
        }
    },
    mounted() {
        this.initColumns();
    }
};
</script>

<style lang="scss" scoped>
.business {
    background: #b6bbbb21;
}
</style>
